package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * 自定义转换器: String -> ItemType
 */
//@Component
public class StringToReleaseStatusConverter implements Converter<String, ReleaseStatus> {

    /**
     * 类型转换
     *
     * @param source
     * @return
     */
    @Override
    public ReleaseStatus convert(String source) {
        for (ReleaseStatus releaseStatus : ReleaseStatus.values()) {
            if (source.equals(String.valueOf(releaseStatus.getCode()))) {
                return releaseStatus;
            }
        }

        // 主动抛出异常
        throw new RuntimeException("传入参数类型错误！必须是1或2");
    }
}
